﻿using Enyim.Caching.Configuration;
using MemcachedDemo.Terminal.Services;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddEnyimMemcached(o => o.Servers = new List<Server>
{
    new Server { Address = "106.53.127.56", Port = 11211 }
});
services.AddSingleton<ICacheProvider, CacheProvider>();
services.AddSingleton<ICacheRepository, CacheRepository>();

using (var serviceProvider = services.BuildServiceProvider(true))
{
    // First Get
    var cacheProvider1 = serviceProvider.GetService<ICacheProvider>();
    var value1 = cacheProvider1.Get("Key_1");
    Console.WriteLine($"Value1 from cache {value1}");

    // Set cache
    Console.WriteLine("Set cache");
    var cacheRepository = serviceProvider.GetService<ICacheRepository>();
    cacheRepository.Set("Key_1", new Dictionary<string, string>()
    {
        {"a","va" },
        {"b","vb" }
    });

    // Sleep for 1 Minutes
    Console.WriteLine("Sleep for 1 minutes");
    Thread.Sleep(1000 * 60);

    // Get cache
    Console.WriteLine("Get cache");
    var cacheProvider = serviceProvider.GetService<ICacheProvider>();
    //var value = cacheProvider.GetCache<string>("Key_1");
    var value = cacheProvider.Get("Key_1");
    Console.WriteLine($"Value from cache {value}");
}

Console.ReadLine();